package yf.mall.vo;

import lombok.Data;
import yf.mall.entity.UserEntity;
import yf.mall.enums.LevelEnum;

import java.math.BigDecimal;

/**
 * 用户奖励明细VO
 */
@Data
public class UserRewardVO {

    private Long userId;

    private String code;

    private String name;

    //级别
    private Long leveId;

    //级别名
    private String leveName;

    private BigDecimal amount;

    private String remark;

    public UserRewardVO(UserEntity user) {
        this.userId = user.getId();
        this.code = user.getCode();
        this.name = user.getName();
        this.leveId = user.getLevelId();
        this.leveName = LevelEnum.levelOf(this.leveId).name;
    }

    //增加金额
    public void addAmount(BigDecimal amount, String remark) {
        if (this.amount == null) {
            this.amount = amount;
        } else {
            this.amount = this.amount.add(amount).setScale(2,BigDecimal.ROUND_DOWN);
        }

        if (this.remark == null) {
            this.remark = remark+"="+this.amount.setScale(2,BigDecimal.ROUND_DOWN);
        } else {
            this.remark += "+" + remark+"="+this.amount.setScale(2,BigDecimal.ROUND_DOWN);
        }

    }

    @Override
    public String toString() {
        String format = "%s-%s-%s-%s";
        if(name!=null){
            name = name.replace("-",""); //去掉- 因为下面需要用- 匹配
            name = name.replace(",",""); //去掉, 因为需要用, 分割
            name = name.trim();
        }else {
            name = "无名";
        }
//        if(remark!=null){
//            remark = remark.replace("-",""); //去掉- 因为下面需要用- 匹配
//            remark = remark.replace(",",""); //去掉, 因为需要用, 分割
//            remark = remark.trim();
//        }else {
//            remark = "";
//        }

        return String.format(format,leveName,code,name,amount.setScale(2,BigDecimal.ROUND_DOWN));
    }

    public UserRewardVO() {
    }

    public static UserRewardVO toUserRewardVO(String info){
        if(info==null) return null;
        String[] strs = info.split("-");
        if(strs.length<4){
            return null;
        }
        UserRewardVO userRewardVO = new UserRewardVO();
        userRewardVO.setLeveName(strs[0]);
        userRewardVO.setCode(strs[1]);
        userRewardVO.setName(strs[2]);
        userRewardVO.setAmount(new BigDecimal(strs[3]));
        return userRewardVO;
    }

    public static void main(String[] args) {
        String info = "247731-btl642869测试号-6-79.68-测试号-9伯乐奖:0.1*398.4000=39.84+测试号-8伯乐奖:0.2*199.2000=79.68,247730-btl302216测试号-5-219.12-测试号-8伯乐奖:0.1*199.2000=19.92+服务利润:0.04*4980.00=219.12,247729-btl231811测试号-4-258.96-测试号-5伯乐奖:0.3*199.2000=59.76+服务利润:0.04*4980.00=258.96,247728-btl738518测试号-3-99.60-测试号-5伯乐奖:0.2*199.2000=39.84+测试号-4伯乐奖:0.3*199.2000=99.60,247734-btl657471测试号-9-398.40-服务利润:0.08*4980.00=398.40,247733-btl773673测试号-8-318.72-测试号-9伯乐奖:0.3*398.4000=119.52+服务利润:0.04*4980.00=318.72,247732-btl861002测试号-7-139.44-测试号-9伯乐奖:0.2*398.4000=79.68+测试号-8伯乐奖:0.3*199.2000=139.44,247736-btl364801测试号-11-398.40-直推奖励:0.08*4980.00=398.40,247727-btl651916测试号2-59.76-测试号-5伯乐奖:0.1*199.2000=19.92+测试号-4伯乐奖:0.2*199.2000=59.76";
       for (String sts : info.split(",")){
           System.out.println(sts);
       }
    }

}
